var notify = (function () {

    $.pnotify.defaults.history = false;

    return {
        success: function (title, text, delay) {
            $.pnotify({
                cornerclass: 'ui-pnotify-sharp',
                delay: delay,
                icon: false,
                nonblock: true,
                nonblock_opacity: .2,
                opacity: .9,
                title: title,
                text: text,
                type: 'success'
            });
        },
        error: function (title, text, delay) {
            $.pnotify({
                cornerclass: 'ui-pnotify-sharp',
                delay: delay,
                icon: false,
                nonblock: true,
                nonblock_opacity: .2,
                opacity: .9,
                title: title,
                text: text,
                type: 'error'
            });
        },
        notice: function (title, text, delay) {
            $.pnotify({
                cornerclass: 'ui-pnotify-sharp',
                delay: delay,
                icon: false,
                nonblock: true,
                nonblock_opacity: .2,
                opacity: .9,
                title: title,
                text: text,
                type: 'notice'
            });
        },
        processing: function () {
            var notification,
                timer;
            function show_notification() {
                notification = $.pnotify({
                    addclass: 'processing',
                    cornerclass: 'ui-pnotify-sharp',
                    hide: false,
                    icon: false,
                    nonblock: true,
                    nonblock_opacity: .2,
                    opacity: .9,
                    text: '',
                    type: 'info',
                    width: '115px',
                    before_open: function (pnotify) {
                        var processing = $('.ui-pnotify.processing .ui-pnotify-text');
                        processing.Loadingdotdotdot({ 'maxDots': 3 });
                    }
                });
                timer = null;
            }
            function show() {
                timer = setTimeout(show_notification, 1000);
            }
            function hide() {
                if (timer) {
                    clearTimeout(timer);
                } else {
                    notification.pnotify_remove();
                }
            }
            return {
                show: show,
                hide: hide
            }
        },
        gridaux: function (tableId, gridFunction) {
            if (typeof grid_aux != "undefined") {
                grid_aux.pnotify_remove();
                //return;
            }
            grid_aux = $.pnotify({
                addclass: 'gridaux',
                closer: false,
                cornerclass: 'ui-pnotify-sharp',
                hide: false,
                icon: false,
                min_height: '332px',
                opacity: .9,
                stack: false,
                sticker: false,
                text: '<table id="' + tableId + '"></table><div id="' + tableId + '_paginacao"></div>',
                type: 'info',
                after_open: function (pnotify) {
                    gridFunction(pnotify);
                },
                before_open: function (pnotify) {
                    pnotify.css({
                        "top": ($(window).height() / 2) - (pnotify.height() / 2),
                        "left": ($(window).width() / 2) - (pnotify.width() / 2)
                    });
                    $('#' + tableId).jqGrid('GridUnload');
                }
            });
            return {
                hide: grid_aux.pnotify_remove
            }
        },
        violations: function (txt) {
            var content = txt.html();
            if (typeof info_box != "undefined") {
                info_box.pnotify_remove();
                //return;
            }
            info_box = $.pnotify({
                closer: false,
                closer_hover: false,
                cornerclass: 'ui-pnotify-sharp',
                delay: 10000,
                icon: false,
                opacity: .9,
                stack: false,
                sticker: false,
                sticker_hover: false,
                text: 'Alguns campos obrigatórios não foram preenchidos ou contém dados inválidos.<br><br>' + content,
                title: 'Problemas Encontrados',
                type: 'notice',
                before_open: function (pnotify) {
                    pnotify.css({
                        'top': ($(window).height() / 2) - (pnotify.height() / 2),
                        'left': ($(window).width() / 2) - (pnotify.width() / 2)
                    }).mouseleave(function () {
                        setTimeout(pnotify.pnotify_remove, 50); 
                    });
                }
            });
        }
    }
} ());

